<?php
class lib_db extends spModel
{
  var $_name = ""; // 数据表的名称
  /**
     * 设置表名称
     * @param var $dname 表名
     */
    public function db($dbname){
       $this->_name = $dbname;
    }
    /**
     * 获取全部数据
     * @param var $field 字段
     * @param var $where 条件
     * @param var $sort 排序
     * @param var $limit 读取记录数量
     */
    public function getAll($field="*",$where,$sort,$limit=""){
        $where = empty($where)? "1=1" : $where;
        $sort = empty($sort)? "id desc" : $sort;
        $limit = empty($limit)? $limit : "limit ".$limit;
        $sql="Select ".$field." from `".$this->_name."` where ".$where." order by ".$sort." ".$limit;
        $result = $this->findsql($sql);
        return $result;
    }

    /**
     * 执行Sql语句
     * @param var $sql sql语句
     */
    public function sql($sql){
        $result = $this->findsql($sql);
        return $result;
    }

    /**
     * 获取一条数据
     * @param var $field 字段
     * @param var $where 条件
     */
    public function getOne($field="*",$where="1=1"){
        $sql="Select ".$field." from `".$this->_name."` where ".$where." limit 1";
        $result = $this->findsql($sql);
        return $result[0];
    }

    /**
     * 获取数据总数
     * @param var $where
     */
    public function getCount($where="1=1"){
        $sql="Select count(*) as num from `".$this->_name."` where ".$where;
        $result = $this->findsql($sql);
        return $result[0]["num"];
    }

    /**
     * 获取数据总数去重
     * @param var $table
     * @param var $where
     */
    public function getCounts($table,$where="1=1"){
        $sql="Select count(".$table.") as num from `".$this->_name."` where ".$where;
        $result = $this->findsql($sql);
        return $result[0]["num"];
    }

    /**
     * 增加一条数据
     * @param array $data
     */
    public function add($data){
        $column = "";
        $datas = "";
        foreach ($data as $key => $one) {
              $one = htmlspecialchars($one);
              $column = $column."`".$key."`,";
              $datas = $datas."'".$one."',";

        }
        $column = $column."//";
        $column =  str_replace(",//", "", $column);
        $datas = $datas."//";
        $datas =  str_replace(",//", "", $datas);
        $sql="insert into `".$this->_name."` (".$column.")values(".$datas.")";
        $result = $this->findsql($sql);
        return $result;
    }

    /**
     * 编辑一条数据
     * @param array $data
     * @param var $where
     */
    public function edit($data,$where="1=1"){
        foreach ($data as $key => $one) {
              $sql = $sql."`".$key."` = '".$one."',";
        }
        $sql = $sql."//";
        $sql =  str_replace(",//", "", $sql);
    	$sql="update `".$this->_name."` set ".$sql." where ".$where;
        $result = $this->findsql($sql);
        return $result;
    }

    /**
     * 删除数据若where为空则删除全部
     * @param var $where
     */
    public function del($where="1=1"){
        $sql="delete from `".$this->_name."` where ".$where;
        $result = $this->findsql($sql);
        return $result;
    }

    /**
     * 获取一条字段数据
     * @param var $field 字段
     * @param var $where 条件
     */
    public function getData($field,$where="1=1",$empty=""){
        $sql="Select ".$field." from `".$this->_name."` where ".$where." limit 1";
        $result = $this->findsql($sql);
        if (!empty($empty) && empty($result[0][$field])){
           return $empty;
        }
        return $result[0][$field];
    }

    /**
     * 获取求和数据
     * @param var $where table
     * @param var $where 条件
     */
    public function getSum($table,$where="1=1"){
        $sql="Select sum(".$table.") as num from `".$this->_name."` where ".$where;
        $result = $this->findsql($sql);
        return $result[0]["num"];
    }
}